package com.lanou.moving.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.adapter.CSongListAdapter;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.entity.dao.AddTo;
import com.lanou.moving.entity.dao.AddToDao;
import com.lanou.moving.fragment.AlbumDetailFragment;
import com.lanou.moving.singleton.DaoSingleton;
import com.lanou.moving.utils.MyConfig;

import java.util.List;

/**
 * Created by 刘琪 on 15/11/6.
 */
public class CollectionSongListActivity extends BaseActivity implements
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnClickListener {
    private ListView local_music_list;
    private List<AddTo> addTos;
    private AddToDao addToDao;
    private AddTo delete_addTo;
    private CSongListAdapter adapter;
    // 标题栏
    private ImageView singer_back, search;
    private TextView singer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_music);
        addToDao = DaoSingleton.getInstance().getAddToDao();
        initView();
        getData();
    }

    public void initView() {
        local_music_list = (ListView) findViewById(R.id.local_music_list);
        local_music_list.setOnItemClickListener(this);
        local_music_list.setOnItemLongClickListener(this);

        singer_back = (ImageView) findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);

        search = (ImageView) findViewById(R.id.search);
        search.setVisibility(View.GONE);

        singer = (TextView) findViewById(R.id.singer);
        singer.setText("收藏的歌单");
    }

    private void getData() {
        addTos = addToDao.loadAll();
        MyConfig.putSharePre(this, "csonglist", "count", addTos.size());
        adapter = new CSongListAdapter(this, addTos);
        local_music_list.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AddTo addTo = (AddTo) parent.getItemAtPosition(position);
        int _id = addTo.getSongListId();
        if (addTo.getResource().equals("歌单")) {
            Intent intent = new Intent(this, SongListDetailActivity.class);
            intent.putExtra("id", _id);
            startActivity(intent);
        } else if (addTo.getResource().equals("发现")) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            AlbumDetailFragment fragment = new AlbumDetailFragment();
            Bundle arg = new Bundle();
            arg.putLong("albumId", (long)_id);
            fragment.setArguments(arg);
            transaction.replace(android.R.id.content, fragment).addToBackStack(null);
            transaction.commit();
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        delete_addTo = (AddTo) parent.getItemAtPosition(position);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("删除歌单");
        builder.setMessage("确定要删除此歌单？");
        builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                addToDao.queryBuilder().where(AddToDao.Properties.SongListId
                        .eq(delete_addTo.getSongListId())).buildDelete()
                        .executeDeleteWithoutDetachingEntities();
                adapter.longClick(delete_addTo);
            }
        });
        builder.setPositiveButton("取消", null);
        builder.show();
        return false;
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
